<?php

class ImportTeachersTask extends sfBaseTask {

    protected function configure() {

        $this->addOptions(array(
            new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
            new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
                // add your own options here
        ));

        $this->namespace = 'import';
        $this->name = 'teachers';
        $this->briefDescription = '';
        $this->detailedDescription = <<<EOF
   imports teachers to sf_guard_user table
EOF;
    }

    protected function execute($arguments = array(), $options = array()) {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
        $q = Doctrine_Query::create()
                ->select('*')
                ->from('Teacher t')
                ->where('t.intg_identity NOT IN (SELECT gu.username from SfGuardUser gu)');
        $out = $q->fetchArray();
        $count = count($out);
        echo "\nfound $count teachers";
        echo "\n";
        if ($count == 0) {
            echo 'nothing to do, aborting ...';
            exit;
        }
        echo "importing ";
        foreach ($out as $one) {
            // -- users
            $gu = new sfGuardUser();
            $gu->set('first_name', $one['f_name']);
            $gu->set('last_name', $one['l_name']);
            $gu->set('username', $one['intg_identity']);
            $gu->set('email_address', $one['intg_identity']);
            $gu->set('is_active', false);
            $gu->set('is_super_admin', false);
            $gu->set('phone', $one['phone']);
            $gu->set('password', 'jKwIpAv*#!');
            $gu->save();
            // -- groups
            $group = new sfGuardUserGroup();
            $group->set('group_id', 5);
            $group->set('user_id', $gu->getId());
            $group->save();
            echo '.';
        }
        echo "\nDone";
    }

}
